package com.xcm.controller;

import com.google.common.collect.Maps;
import com.xcm.constant.BusinessConstant;
import com.xcm.constant.ResponseConstant;
import com.xcm.model.dto.DeviceGroupListDTO;
import com.xcm.model.dto.DeviceModelDTO;
import com.xcm.model.vo.DeviceInfoListVO;
import com.xcm.model.vo.FirmWareListVO;
import com.xcm.page.PageInfo;
import com.xcm.service.DeviceGroupService;
import com.xcm.service.DeviceModelService;
import com.xcm.service.FirmWareService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.Map;


/**
 * @ClassName FirmWareController
 * @Description 固件管理控制类
 * @Author HanQinrui
 * @Date 2018/5/7 9:42
 * @Version 1.0
 **/
@Controller
@RequestMapping(value = "/firmWare")
public class FirmWareController {
    private static Logger logger = LoggerFactory.getLogger(FirmWareController.class);
    @Autowired
    private FirmWareService firmWareService;
    @Autowired
    private DeviceModelService deviceModelService;
    @Autowired
    private DeviceGroupService deviceGroupService;
    /**
     * 固件分页列表
     * @param pageNum
     * @param pageSize
     * @param model
     * @return
     */
    @RequestMapping(value = "/listByPage")
    public Object listByPage(Integer pageNum, Integer pageSize, Model model) {
        PageInfo<FirmWareListVO> pageIno = firmWareService.listByPage(pageNum, pageSize);
        model.addAttribute("pageNum", pageIno.getPageNum());
        model.addAttribute("pageSize", pageIno.getPageSize());
        model.addAttribute("total", pageIno.getTotal());
        model.addAttribute("resultList", pageIno.getList());
        return "firmware-list";
    }

    /**
     * 跳转更新固件
     * @param sysVersion 固件版本号
     * @param deviceStatus 在线状态 0.在线; 1.离线
     * @param deviceModel 机型
     * @param groupCode 分组编码
     * @param province 省
     * @param city 市
     * @param area 区
     * @param deviceUpgradeId 固件ID
     * @param pageNum
     * @param pageSize
     * @param model
     * @return
     */
    @RequestMapping(value = "/updateFirmWareForward")
    public Object updateFirmWareForward(String sysVersion, String deviceStatus, String deviceModel, String groupCode,
                                        String province,String city, String area, String deviceUpgradeId,
                                        Integer pageNum, Integer pageSize, Model model) {
        if(StringUtils.isBlank(deviceUpgradeId)) {
            model.addAttribute("errorCode",ResponseConstant.CODE_FAIL);
            model.addAttribute("errorMsg",ResponseConstant.MSG_PARAM_ERROR);
            return "firmware_upgrade";
        }

        // 查询条件回显
        model.addAttribute("sysVersion",sysVersion);
        model.addAttribute("deviceStatus",deviceStatus);
        model.addAttribute("deviceModel",deviceModel);
        model.addAttribute("groupCode",groupCode);
        model.addAttribute("province",province);
        model.addAttribute("city",city);
        model.addAttribute("area",area);
        model.addAttribute("deviceUpgradeId",deviceUpgradeId);

        // 查询所有固件版本号
        List<String> versionCodeList = firmWareService.listAllFirmWareVersionCode();
        model.addAttribute("versionCodeList",versionCodeList);
        //在线状态
        Map<Object, Object> deviceStatusMap = Maps.newHashMap();
        deviceStatusMap.put(BusinessConstant.DEVICE_INFO_STATUS_ON_LINE, "在线");
        deviceStatusMap.put(BusinessConstant.DEVICE_INFO_STATUS_OFF_LINE, "离线");
        model.addAttribute("deviceStatusMap", deviceStatusMap);
        //机型
        List<DeviceModelDTO> deviceModelList = deviceModelService.listDeviceModels();
        model.addAttribute("deviceModelList", deviceModelList);
        //分组
        List<DeviceGroupListDTO> deviceGroupList = deviceGroupService.listAllDeviceGroup();
        model.addAttribute("deviceGroupList", deviceGroupList);
        //在线终端总数
        Long onlineCount = firmWareService.getFirmWareDeviceOnlineCount(sysVersion,deviceModel, groupCode, province, city, area);
        model.addAttribute("onlineCount", onlineCount);
        //离线终端总数
        Long offlineCount = firmWareService.getFirmWareDeviceOfflineCount(sysVersion, deviceModel, groupCode, province, city, area);
        model.addAttribute("offlineCount",offlineCount);

        // 查询终端列表信息
        PageInfo<DeviceInfoListVO> pageIno = firmWareService.updateFirmWareListByPage(
                sysVersion, deviceStatus, deviceModel, groupCode,
                province, city, area, deviceUpgradeId, pageNum, pageSize);
        model.addAttribute("pageNum", pageIno.getPageNum());
        model.addAttribute("pageSize", pageIno.getPageSize());
        model.addAttribute("total", pageIno.getTotal());
        model.addAttribute("resultList", pageIno.getList());
        model.addAttribute("errorCode",ResponseConstant.CODE_SUCCESS);
        return "firmware_upgrade";
    }
}
